﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
using System.IO;
public partial class Admin_SanPhamList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            Load_DropdownList();
            load_data();
        }
    }
    protected void load_data()
    {
        PagerV2_8_1 .PageSize = 5;
        Criteria c = new Criteria();
        if (txt_TenSP.Text != "")
        {
            c.add(Condition.AND, tbSanPhamColumns.TenSP.ToString (), Condition.LIKE, txt_TenSP .Text);
        }
        if (txt_GiaBan .Text != "")
        {
            c.add(Condition.AND, tbSanPhamColumns.GiaBan.ToString (), Condition.LIKE, txt_GiaBan.Text);
        }
        if (ddl_LoaiSP.SelectedValue != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns.IdLoaiSP.ToString (), Condition.LIKE, int.Parse(ddl_LoaiSP.SelectedValue));
        }
        int total = 0;
        tbSanPhamCollection sanphamcolection =
        tbSanPhamBUS.Get(c.Criter.ToString(), PagerV2_8_1.CurrentIndex, PagerV2_8_1.PageSize,
        tbSanPhamColumns.IdSanPham.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = sanphamcolection;
        Repeater1.DataBind();
        PagerV2_8_1.ItemCount = total;
        if (total <= PagerV2_8_1.PageSize)
        {
            PagerV2_8_1.Visible = false;
        }

    }
    protected void Load_DropdownList()
    {
        tbLoaiSPCollection loaispColection = tbLoaiSPBUS.GetAll();
        ddl_LoaiSP.DataTextField = tbLoaiSPColumns.TenLoai.ToString();
        ddl_LoaiSP.DataValueField = tbLoaiSPColumns.IdLoaiSP.ToString();
        ddl_LoaiSP.DataSource = loaispColection;
        ddl_LoaiSP.DataBind();
        ddl_LoaiSP.Items.Insert(0, new ListItem("All", "0"));
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbSanPham sanPham = (tbSanPham)e.Item.DataItem;

            HyperLink hyp_TenSP = (HyperLink)e.Item.FindControl("hyp_TenSP");
            Literal lit_LoaiSP = (Literal)e.Item.FindControl("lit_LoaiSP");
            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            hyp_TenSP.Text = sanPham.TenSP;
            hyp_TenSP.NavigateUrl = "SanPhamDetail.aspx?id=" + sanPham.IdSanPham.ToString();

            tbLoaiSP loaiSP=tbLoaiSPBUS .GetId (int.Parse (sanPham .IdLoaiSP.ToString ()));
            lit_LoaiSP.Text = loaiSP.TenLoai;
            int Id = 0;
            int MaHA = tbHinhAnhBUS.FirstImage (int.Parse(sanPham.IdSanPham.ToString()),Id);
            if (MaHA > 0)
            {
                tbHinhAnh hinhAnh = tbHinhAnhBUS.GetById(MaHA);
                img_HinhAnh.ImageUrl = "~/Photos/Thumb/" + hinhAnh.TenHinh.ToString();
            }
            else
            {
                img_HinhAnh.ImageUrl = "~/Photos/Thumb/no_images.jpg";
            }
                HyperLink hyp_Edit = (HyperLink)e.Item.FindControl("hyp_Edit");
                hyp_Edit.NavigateUrl = "SanPhamEdit.aspx?id=" + sanPham.IdSanPham.ToString();
                hf_id.Value = sanPham.IdSanPham.ToString(); 
            
        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {

        PagerV2_8_1 .CurrentIndex = int.Parse(e.CommandArgument.ToString());
        load_data();
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        load_data();
    }
    protected void lbt_Delete_Click(object sender, EventArgs e)
    {
       
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
        
            CheckBox chk = (CheckBox)item.FindControl("cb_xoa");
            if (chk.Checked == true)
            {
               HiddenField hf_id = (HiddenField)item.FindControl("hf_id");
               Criteria cri = new Criteria();
               int Total = 0;
               cri.add(Condition.AND, tbCTDonHangColumns.IdSanPham.ToString(), Condition.EQUAL, int.Parse(hf_id.Value));
               tbCTDonHangCollection  ctdhCollection = tbCTDonHangBUS .Get(cri.Criter, 0, 0, tbDonHangColumns.IdDH.ToString(),
                                                                    OrderDirection.ASC.ToString(), out Total);
               if (ctdhCollection.Count > 0)
               {
                 ShowMessage("không thể xóa sản phẩm này");
               }
               else
               {
                 //gan gia tri cho mang ten hinh
                 string[] array = tbHinhAnhBUS.DeleteImageByIdSP(int.Parse(hf_id.Value));

                 //xoa hinh anh cua san pham
                 tbHinhAnhBUS.DeleteIdSP(int.Parse(hf_id.Value));

                 //xoa san pham
                 tbSanPhamBUS.Delete_SP(int.Parse(hf_id.Value));

                 //xoa hinh trong thu muc Photos
                 foreach (string tenhinh in array)
                 {
                    if (tenhinh == null)
                    {
                       break;
                    }
                    else
                    {
                       File.Delete(Server.MapPath("~/Photos/" + tenhinh.ToString()));
                       File.Delete(Server.MapPath("~/Photos/Thumb/" + tenhinh.ToString()));
                    }
                 }
               }
            }
         }
         load_data();
    }
    public void ShowMessage(string Content)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('" + Content + "');</script>");
    }
}
